//
//  PiePolylineChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "PiePolylineChartViewController.h"
@interface PiePolylineChartViewController ()

@end

@implementation PiePolylineChartViewController
PieChartView *samplePiePolylineChartView;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initPieChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initPieChartView{
    samplePiePolylineChartView=[[PieChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    samplePiePolylineChartView.backgroundColor = [UIColor whiteColor];
    samplePiePolylineChartView.delegate = self;
    [self.view addSubview:samplePiePolylineChartView];
    [self initPieChartStyleAndData];
}

-(void)initPieChartStyleAndData{
    [self setStyle:samplePiePolylineChartView];
    [self setHollowStyle:samplePiePolylineChartView];
    [self setDrawHoleText:samplePiePolylineChartView];
    [self setLegendStyle:samplePiePolylineChartView];
     samplePiePolylineChartView.legend.enabled = NO;
    [self setDescription:samplePiePolylineChartView];
    [self initChartData];
}

// 基本样式
-(void)setStyle:(PieChartView *)chartView{
    [chartView setExtraOffsetsWithLeft:20.f top:0.f right:20.f bottom:0.f];
    //[chartView animateWithYAxisDuration:1.4 easingOption:ChartEasingOptionEaseOutBack];
}

//饼状图中间的空心样式
-(void)setHollowStyle:(PieChartView *)chartView{
    //饼状图是否是空心
    chartView.drawHoleEnabled = YES;
    //空心半径占比
    chartView.holeRadiusPercent = 0.5;
    //空心颜色
    chartView.holeColor = [UIColor clearColor];
    //半透明空心半径占比
    chartView.transparentCircleRadiusPercent = 0.52;
    //半透明空心的颜色
    chartView.transparentCircleColor = [UIColor colorWithRed:210/255.0 green:145/255.0 blue:165/255.0 alpha:0.3];
}

//设置饼状图中心的文本
-(void)setDrawHoleText:(PieChartView *)chartView{
    if (chartView.isDrawHoleEnabled == YES) {
        chartView.drawCenterTextEnabled = YES;//是否显示中间文字
        //普通文本
        //        samplePieChartView.centerText = @"饼状图";//中间文字
        //富文本
        NSMutableAttributedString *centerText = [[NSMutableAttributedString alloc] initWithString:@"饼状图"];
        [centerText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
                                    NSForegroundColorAttributeName: [UIColor orangeColor]}
                            range:NSMakeRange(0, centerText.length)];
        chartView.centerAttributedText = centerText;
    }
}

//设置饼状图描述
-(void)setDescription:(PieChartView *)chartView{
    chartView.descriptionText = @"饼状图示例";
    chartView.descriptionFont = [UIFont systemFontOfSize:10];
    chartView.descriptionTextColor = [UIColor grayColor];
}

//设置饼状图图例样式
-(void)setLegendStyle:(PieChartView *)chartView{
    //图例在饼状图中的大小占比, 这会影响图例的宽高
    chartView.legend.maxSizePercent = 1;
    //文本间隔
    chartView.legend.formToTextSpace = 5;
    //字体大小
    chartView.legend.font = [UIFont systemFontOfSize:10];
    //字体颜色
    chartView.legend.textColor = [UIColor grayColor];
    //图例在饼状图中的位置
    chartView.legend.position = ChartLegendPositionBelowChartCenter;
    //图示样式: 方形、线条、圆形
    chartView.legend.form = ChartLegendFormCircle;
    //图示大小
    chartView.legend.formSize = 12;
}

- (void)initChartData
{
     [self setDataCount:3 range:100 chart:samplePiePolylineChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(PieChartView *)chartView
{
    double mult = range;
    
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    
    // IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
    for (int i = 0; i < count; i++)
    {
        [yVals1 addObject:[[BarChartDataEntry alloc] initWithValue:(arc4random_uniform(mult) + mult / 5) xIndex:i]];
    }
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < count; i++)
    {
        [xVals addObject:[@(i) stringValue]];
    }
    
    PieChartDataSet *dataSet = [[PieChartDataSet alloc] initWithYVals:yVals1 label:@"Election Results"];
    dataSet.sliceSpace = 2.0;
    
    // add a lot of colors
    
    NSMutableArray *colors = [[NSMutableArray alloc] init];
    [colors addObjectsFromArray:ChartColorTemplates.vordiplom];
    [colors addObjectsFromArray:ChartColorTemplates.joyful];
    [colors addObjectsFromArray:ChartColorTemplates.colorful];
    [colors addObjectsFromArray:ChartColorTemplates.liberty];
    [colors addObjectsFromArray:ChartColorTemplates.pastel];
    [colors addObject:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
    
    dataSet.colors = colors;
    
    dataSet.valueLinePart1OffsetPercentage = 0.8;
    dataSet.valueLinePart1Length = 0.2;
    dataSet.valueLinePart2Length = 0.4;
    //dataSet.xValuePosition = PieChartValuePositionOutsideSlice;
    dataSet.yValuePosition = PieChartValuePositionOutsideSlice;
    
    PieChartData *data = [[PieChartData alloc] initWithXVals:xVals dataSet:dataSet];
    
    NSNumberFormatter *pFormatter = [[NSNumberFormatter alloc] init];
    pFormatter.numberStyle = NSNumberFormatterPercentStyle;
    pFormatter.maximumFractionDigits = 1;
    pFormatter.multiplier = @1.f;
    pFormatter.percentSymbol = @" %";
    [data setValueFormatter:pFormatter];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11.f]];
    [data setValueTextColor:UIColor.blackColor];
    
    chartView.data = data;
    [chartView highlightValues:nil];
}

#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end